<?php
/**
 * @file functions.php
 * @brief Provides a set of general functions
 */
function loadJS($src){
	echo '<script type="text/javascript" src="'.$src.'"></script>';
}
function writeScript($script){
	echo '<script type="text/javascript">';
	echo $script;
	echo '</script>';
}


/* 
	$insertstring - the string you want to insert
	$intostring - the string you want to insert it into
	$offset - the offset
*/
function addClass($newclass, $attr) {
	$pos = strpos($attr,'class="');
	if($pos === false){
		$offset = 0;
		$newclass = 'class="'.$newclass.'"';
	}else{
		$offset = $pos+7;
	}
   $part1 = substr($attr, 0, $offset);
   $part2 = substr($attr, $offset);
  
   $part1 = $part1 . $newclass.' ';
   $whole = $part1 . $part2;
   return $whole;
}
/**
check if a string starts with $prefix

@param $string
	The target string

@param $prefix
	string to be checked if the target string has as a start-with

@return 
	true if string starts with $prefix
*/
function starts_with($string, $prefix){
    // returns a bool
    return strpos($string, $prefix) === 0;
}

/**
convert a number to 2 decimal places

@param $number
	the number to be converted.

*/
function toMoneyFormat($number){
	return number_format((float)$number,2);
}

/**
clean up the values in an array

@param $post_array
	array with values to be cleaned
	
@return
	the cleaned array

*/
function cleanPost($post_array){
	foreach($post_array as $key => $value){
		$post_array[$key] = urldecode(trim($value));
	}
	return $post_array;
}

/**
redirect to another page using javascript
@param $msg
	message to display when redirecting

@param $url
	url to redirect to

*/
function js_redirect($msg, $url){
	echo $msg.'<br />';
	echo 'If your browser did not redirect automatically, please <a href="'.$url.'">click here</a>';
	echo '<script type="text/javascript">setTimeout(function(){window.location = "'.$url.'";},1000);</script>';
}

/**
check if an email is in valid format
@return 
	true - email format valid
	false - email format not valid

*/
function validEmail($email){
	return filter_var($email, FILTER_VALIDATE_EMAIL);
}

/**
creates text input element for form
@param $label
	the label text for this text field
@param $name
	the name for this text field, also the id for this element
@param $required
	boolean value, if this field has required attribute, default false 
@param $default_value
	default value for this text field
*/
function createTextInputField($label, $name, $required=false, $default_value = ''){
	echo '<label for="'.$name.'">'.$label.'</label>';
	$required_section = ($required) ? ' required="required" ':'';
	echo '<input type="text" id="'.$name.'" name="'.$name.'" value="'.$default_value.'" '.$required_section.' />';
}

/**
short writing for $_POST data
@param $var
	the variable name for post data
*/
function postvar($var){
	if(isset($_POST[$var])){
		return trim($_POST[$var]);
	}
}
/**
generates a random 10-digit string
*/
function generateKey($length = 10){
    return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}

/**
	output a line of text with the <br /> tag
*/
function writeln($msg=''){
	echo $msg.'<br />';
}

/**
	customized send email function
*/
function sendEmail($recipient, $recipient_name, $from, $from_name, $subject, $message, $attachments=null,$is_important=false){
	require(ROOT_DIR.'/lib/PHPMailer/class.phpmailer.php');
	$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
	$mail->IsSendmail(); // telling the class to use SendMail transport
	
	try {
		
	  //$mail->AddReplyTo('name@yourdomain.com', 'First Last');
	  $mail->AddAddress($recipient, $recipient_name);
	  $mail->SetFrom($from, $from_name);
	  if($is_important){
		  $mail->Priority = 1;
		  $mail->addCustomHeader("X-Priority: 1");
		  $mail->addCustomHeader("X-MSMail-Priority: High");
		  $mail->addCustomHeader("Importance: High");
	  }
	  $mail->Subject = $subject;	  
	  $mail->MsgHTML($message);
	  if($attachments != null){
		  foreach($attachments as $attachment){
			  $mail->AddAttachment($attachment);      // attachment
		  }
	  }
	  $mail->Send();
	  
	  return true;
	} catch (phpmailerException $e) {
	  echo $e->errorMessage(); //Pretty error messages from PHPMailer
	  return false;
	} catch (Exception $e) {
	  echo $e->getMessage(); //Boring error messages from anything else!
	  return false;
	}
}

?>